﻿#ifndef BLUETOOTH_DEVICE_AGENT_H
#define BLUETOOTH_DEVICE_AGENT_H

#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Foundation.h>
#include <map>
#include <string>
#include <functional>
#include <memory>

namespace winrt {
    using namespace Windows::Devices::Bluetooth;
    using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
    using namespace Windows::Foundation;
}
//定义MTU变化回调函数类型
using MaxPduSizeChangedCallback = std::function<void(int)>;

struct BluetoothDeviceAgent {
    //蓝牙设备
    winrt::BluetoothLEDevice device;
    //GATT会话
    winrt::GattSession gattSession{ nullptr };
    //连接状态变化事件令牌
    winrt::event_token connectionStatusChangedToken;
    //服务与特征的缓存
    std::map<winrt::hstring, winrt::GattDeviceService> gattServices;
    std::map<winrt::hstring, std::map<winrt::hstring, winrt::GattCharacteristic>> gattServicesCharacteristics;
    //特征值变化的事件令牌
    std::map<winrt::hstring, std::map<winrt::hstring, winrt::event_token>> valueChangedTokens;
    //特征值变化的事件令牌
    MaxPduSizeChangedCallback onMtuChangeCallback;

    //构造函数
    BluetoothDeviceAgent(winrt::BluetoothLEDevice device, winrt::event_token connectionStatusChangedToken, MaxPduSizeChangedCallback callback = nullptr);
    //析构函数
    ~BluetoothDeviceAgent();
    //关闭设备连接
    void Close();
    //初始化GATT会话
    winrt::IAsyncAction InitGattSession();
    //加载所有服务与特征
    winrt::IAsyncAction LoadInit();
    //加载所以服务
    winrt::IAsyncAction LoadAllServiceAsync();
    //加载服务对应的所以特征
    winrt::IAsyncAction LoadAllCharacteristicAsync(const winrt::hstring& service);
    //获取指定服务
    winrt::IAsyncOperation<winrt::GattDeviceService> GetServiceAsync(const winrt::hstring& service);
    //获取指定特征
    winrt::IAsyncOperation<winrt::GattCharacteristic> GetCharacteristicAsync(const winrt::hstring& service, const winrt::hstring& characteristic);
};

#endif // BLUETOOTH_DEVICE_AGENT_H